package com.farsight.leo.fssmartlock.bases;

import android.app.Application;
import android.content.Context;

import com.farsight.leo.fssmartlock.db.DeviceDb;
import com.farsight.leo.fssmartlock.net.bean.UserData;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.Room;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

public class BaseApplication extends Application {
    private static android.os.Handler handler;
    private static UserData userData;
    private static Context context;
    private static DeviceDb deviceDb;


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        handler = new android.os.Handler();
        deviceDb = Room.databaseBuilder(this, DeviceDb.class, "fs_lock.db")
                .fallbackToDestructiveMigration().allowMainThreadQueries().build();
        //        deviceDb = Room.inMemoryDatabaseBuilder(this, DeviceDb.class)
        //                .allowMainThreadQueries().build();
    }

    private Migration migration=new Migration(2,3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
//            @Entity(tableName = "tbl_devices",indices = {@Index(value = {"user_id","user_name","mac_address"},unique = true)})
//            database.execSQL("drop index user_id on tbl_devices (user_id);");
        }
    };

    public static void post(Runnable runnable) {
        handler.post(runnable);
    }

    public static void postDelay(Runnable runnable, long millis) {
        handler.postDelayed(runnable, millis);
    }

    public static void setUserData(UserData user) {
        userData = user;
    }

    public static UserData getUserData() {
        return userData;
    }

    public static Context acquireBaseContext() {
        return context;
    }

    public static DeviceDb getDeviceDb() {
        return deviceDb;
    }
}
